﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace LMHTX.Modules.DIRECTORY.COOPERATIVE.Components
{
    public partial class History
    {
        #region Private members.

        protected int _ID;
        protected int _CooperativeID;
        protected int _Year;
        protected decimal _CharterCapital;
        protected decimal _WorkingCapital;
        protected decimal _FixedCapital;
        protected decimal _Turnover;
        protected decimal _Tax;
        protected decimal _Profit;
        protected int _LabourQuantity;
        protected int _WomanLabourQuantity;
        protected int _CooperatorQuantity;
        protected int _WomanCooperatorQuantity;
        protected int _PartyMemberQuantity;
        protected string _Rank = string.Empty;
        protected int _StatusID;

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public int ID
        {
            set { this._ID = value; }
            get { return this._ID; }
        }

        public int CooperativeID
        {
            set { this._CooperativeID = value; }
            get { return this._CooperativeID; }
        }

        public int Year
        {
            set { this._Year = value; }
            get { return this._Year; }
        }

        public decimal CharterCapital
        {
            set { this._CharterCapital = value; }
            get { return this._CharterCapital; }
        }

        public decimal WorkingCapital
        {
            set { this._WorkingCapital = value; }
            get { return this._WorkingCapital; }
        }

        public decimal FixedCapital
        {
            set { this._FixedCapital = value; }
            get { return this._FixedCapital; }
        }

        public decimal Turnover
        {
            set { this._Turnover = value; }
            get { return this._Turnover; }
        }

        public decimal Tax
        {
            set { this._Tax = value; }
            get { return this._Tax; }
        }

        public decimal Profit
        {
            set { this._Profit = value; }
            get { return this._Profit; }
        }

        public int LabourQuantity
        {
            set { this._LabourQuantity = value; }
            get { return this._LabourQuantity; }
        }

        public int WomanLabourQuantity
        {
            set { this._WomanLabourQuantity = value; }
            get { return this._WomanLabourQuantity; }
        }

        public int CooperatorQuantity
        {
            set { this._CooperatorQuantity = value; }
            get { return this._CooperatorQuantity; }
        }

        public int WomanCooperatorQuantity
        {
            set { this._WomanCooperatorQuantity = value; }
            get { return this._WomanCooperatorQuantity; }
        }

        public int PartyMemberQuantity
        {
            set { this._PartyMemberQuantity = value; }
            get { return this._PartyMemberQuantity; }
        }

        public string Rank
        {
            set { this._Rank = value; }
            get { return this._Rank; }
        }

        public int StatusID
        {
            set { this._StatusID = value; }
            get { return this._StatusID; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static History Load(int id)
        {
            const string spName = "[dbo].[p_COOPERATIVE_History_Load]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, id);
            History entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new History();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CooperativeID"))) entity.CooperativeID = reader.GetInt32(reader.GetOrdinal("CooperativeID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Year"))) entity.Year = reader.GetInt32(reader.GetOrdinal("Year"));
                if (!reader.IsDBNull(reader.GetOrdinal("CharterCapital"))) entity.CharterCapital = reader.GetDecimal(reader.GetOrdinal("CharterCapital"));
                if (!reader.IsDBNull(reader.GetOrdinal("WorkingCapital"))) entity.WorkingCapital = reader.GetDecimal(reader.GetOrdinal("WorkingCapital"));
                if (!reader.IsDBNull(reader.GetOrdinal("FixedCapital"))) entity.FixedCapital = reader.GetDecimal(reader.GetOrdinal("FixedCapital"));
                if (!reader.IsDBNull(reader.GetOrdinal("Turnover"))) entity.Turnover = reader.GetDecimal(reader.GetOrdinal("Turnover"));
                if (!reader.IsDBNull(reader.GetOrdinal("Tax"))) entity.Tax = reader.GetDecimal(reader.GetOrdinal("Tax"));
                if (!reader.IsDBNull(reader.GetOrdinal("Profit"))) entity.Profit = reader.GetDecimal(reader.GetOrdinal("Profit"));
                if (!reader.IsDBNull(reader.GetOrdinal("LabourQuantity"))) entity.LabourQuantity = reader.GetInt32(reader.GetOrdinal("LabourQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("WomanLabourQuantity"))) entity.WomanLabourQuantity = reader.GetInt32(reader.GetOrdinal("WomanLabourQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("CooperatorQuantity"))) entity.CooperatorQuantity = reader.GetInt32(reader.GetOrdinal("CooperatorQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("WomanCooperatorQuantity"))) entity.WomanCooperatorQuantity = reader.GetInt32(reader.GetOrdinal("WomanCooperatorQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("PartyMemberQuantity"))) entity.PartyMemberQuantity = reader.GetInt32(reader.GetOrdinal("PartyMemberQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("Rank"))) entity.Rank = reader.GetString(reader.GetOrdinal("Rank"));
                if (!reader.IsDBNull(reader.GetOrdinal("StatusID"))) entity.StatusID = reader.GetInt32(reader.GetOrdinal("StatusID"));
            }
            reader.Close();
            return entity;
        }

        //---------------------------------------------------------------------------------------------
        public static List<History> SelectCollectionAll()
        {
            List<History> collection = new List<History>();
            SqlDataReader reader = (SqlDataReader)SelectReaderAll();
            while (reader.Read())
            {
                History entity = new History();

                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CooperativeID"))) entity.CooperativeID = reader.GetInt32(reader.GetOrdinal("CooperativeID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Year"))) entity.Year = reader.GetInt32(reader.GetOrdinal("Year"));
                if (!reader.IsDBNull(reader.GetOrdinal("CharterCapital"))) entity.CharterCapital = reader.GetDecimal(reader.GetOrdinal("CharterCapital"));
                if (!reader.IsDBNull(reader.GetOrdinal("WorkingCapital"))) entity.WorkingCapital = reader.GetDecimal(reader.GetOrdinal("WorkingCapital"));
                if (!reader.IsDBNull(reader.GetOrdinal("FixedCapital"))) entity.FixedCapital = reader.GetDecimal(reader.GetOrdinal("FixedCapital"));
                if (!reader.IsDBNull(reader.GetOrdinal("Turnover"))) entity.Turnover = reader.GetDecimal(reader.GetOrdinal("Turnover"));
                if (!reader.IsDBNull(reader.GetOrdinal("Tax"))) entity.Tax = reader.GetDecimal(reader.GetOrdinal("Tax"));
                if (!reader.IsDBNull(reader.GetOrdinal("Profit"))) entity.Profit = reader.GetDecimal(reader.GetOrdinal("Profit"));
                if (!reader.IsDBNull(reader.GetOrdinal("LabourQuantity"))) entity.LabourQuantity = reader.GetInt32(reader.GetOrdinal("LabourQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("WomanLabourQuantity"))) entity.WomanLabourQuantity = reader.GetInt32(reader.GetOrdinal("WomanLabourQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("CooperatorQuantity"))) entity.CooperatorQuantity = reader.GetInt32(reader.GetOrdinal("CooperatorQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("WomanCooperatorQuantity"))) entity.WomanCooperatorQuantity = reader.GetInt32(reader.GetOrdinal("WomanCooperatorQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("PartyMemberQuantity"))) entity.PartyMemberQuantity = reader.GetInt32(reader.GetOrdinal("PartyMemberQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("Rank"))) entity.Rank = reader.GetString(reader.GetOrdinal("Rank"));
                if (!reader.IsDBNull(reader.GetOrdinal("StatusID"))) entity.StatusID = reader.GetInt32(reader.GetOrdinal("StatusID"));
                collection.Add(entity);
            }

            reader.Close();
            return collection;
        }

        //---------------------------------------------------------------------------------------------

        public static List<History> SelectCollectionDynamic(string whereCondition, string orderByExpression)
        {
            List<History> collection = new List<History>();

            SqlDataReader reader = (SqlDataReader)SelectReaderDynamic(whereCondition, orderByExpression);
            while (reader.Read())
            {
                History entity = new History();

                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CooperativeID"))) entity.CooperativeID = reader.GetInt32(reader.GetOrdinal("CooperativeID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Year"))) entity.Year = reader.GetInt32(reader.GetOrdinal("Year"));
                if (!reader.IsDBNull(reader.GetOrdinal("CharterCapital"))) entity.CharterCapital = reader.GetDecimal(reader.GetOrdinal("CharterCapital"));
                if (!reader.IsDBNull(reader.GetOrdinal("WorkingCapital"))) entity.WorkingCapital = reader.GetDecimal(reader.GetOrdinal("WorkingCapital"));
                if (!reader.IsDBNull(reader.GetOrdinal("FixedCapital"))) entity.FixedCapital = reader.GetDecimal(reader.GetOrdinal("FixedCapital"));
                if (!reader.IsDBNull(reader.GetOrdinal("Turnover"))) entity.Turnover = reader.GetDecimal(reader.GetOrdinal("Turnover"));
                if (!reader.IsDBNull(reader.GetOrdinal("Tax"))) entity.Tax = reader.GetDecimal(reader.GetOrdinal("Tax"));
                if (!reader.IsDBNull(reader.GetOrdinal("Profit"))) entity.Profit = reader.GetDecimal(reader.GetOrdinal("Profit"));
                if (!reader.IsDBNull(reader.GetOrdinal("LabourQuantity"))) entity.LabourQuantity = reader.GetInt32(reader.GetOrdinal("LabourQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("WomanLabourQuantity"))) entity.WomanLabourQuantity = reader.GetInt32(reader.GetOrdinal("WomanLabourQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("CooperatorQuantity"))) entity.CooperatorQuantity = reader.GetInt32(reader.GetOrdinal("CooperatorQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("WomanCooperatorQuantity"))) entity.WomanCooperatorQuantity = reader.GetInt32(reader.GetOrdinal("WomanCooperatorQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("PartyMemberQuantity"))) entity.PartyMemberQuantity = reader.GetInt32(reader.GetOrdinal("PartyMemberQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("Rank"))) entity.Rank = reader.GetString(reader.GetOrdinal("Rank"));
                if (!reader.IsDBNull(reader.GetOrdinal("StatusID"))) entity.StatusID = reader.GetInt32(reader.GetOrdinal("StatusID"));
                collection.Add(entity);
            }

            reader.Close();
            return collection;
        }

        //---------------------------------------------------------------------------------------------

        // Select by foreign key return collection		
        public static IList<History> SelectCollectionBy_CooperativeID(int cooperativeID)
        {
            List<History> collection = new List<History>();
            SqlDataReader reader = (SqlDataReader)SelectReaderBy_CooperativeID(cooperativeID);
            while (reader.Read())
            {
                History entity = new History();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CooperativeID"))) entity.CooperativeID = reader.GetInt32(reader.GetOrdinal("CooperativeID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Year"))) entity.Year = reader.GetInt32(reader.GetOrdinal("Year"));
                if (!reader.IsDBNull(reader.GetOrdinal("CharterCapital"))) entity.CharterCapital = reader.GetDecimal(reader.GetOrdinal("CharterCapital"));
                if (!reader.IsDBNull(reader.GetOrdinal("WorkingCapital"))) entity.WorkingCapital = reader.GetDecimal(reader.GetOrdinal("WorkingCapital"));
                if (!reader.IsDBNull(reader.GetOrdinal("FixedCapital"))) entity.FixedCapital = reader.GetDecimal(reader.GetOrdinal("FixedCapital"));
                if (!reader.IsDBNull(reader.GetOrdinal("Turnover"))) entity.Turnover = reader.GetDecimal(reader.GetOrdinal("Turnover"));
                if (!reader.IsDBNull(reader.GetOrdinal("Tax"))) entity.Tax = reader.GetDecimal(reader.GetOrdinal("Tax"));
                if (!reader.IsDBNull(reader.GetOrdinal("Profit"))) entity.Profit = reader.GetDecimal(reader.GetOrdinal("Profit"));
                if (!reader.IsDBNull(reader.GetOrdinal("LabourQuantity"))) entity.LabourQuantity = reader.GetInt32(reader.GetOrdinal("LabourQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("WomanLabourQuantity"))) entity.WomanLabourQuantity = reader.GetInt32(reader.GetOrdinal("WomanLabourQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("CooperatorQuantity"))) entity.CooperatorQuantity = reader.GetInt32(reader.GetOrdinal("CooperatorQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("WomanCooperatorQuantity"))) entity.WomanCooperatorQuantity = reader.GetInt32(reader.GetOrdinal("WomanCooperatorQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("PartyMemberQuantity"))) entity.PartyMemberQuantity = reader.GetInt32(reader.GetOrdinal("PartyMemberQuantity"));
                if (!reader.IsDBNull(reader.GetOrdinal("Rank"))) entity.Rank = reader.GetString(reader.GetOrdinal("Rank"));
                if (!reader.IsDBNull(reader.GetOrdinal("StatusID"))) entity.StatusID = reader.GetInt32(reader.GetOrdinal("StatusID"));
                collection.Add(entity);
            }
            reader.Close();
            return collection;
        }
        //---------------------------------------------------------------------------------------------

        public static DataSet SelectBy_CooperativeID(int cooperativeID)
        {
            const string spName = "[dbo].[p_COOPERATIVE_History_SelectBy_CooperativeID]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CooperativeID", SqlDbType.Int, cooperativeID);

            return db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------

        public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_COOPERATIVE_History_SelectAll]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_COOPERATIVE_History_SelectDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_COOPERATIVE_History_SelectAll]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_COOPERATIVE_History_SelectDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        // Select by foreign key return collection		
        public static IDataReader SelectReaderBy_CooperativeID(int cooperativeID)
        {
            const string spName = "p_COOPERATIVE_History_SelectBy_CooperativeID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CooperativeID", SqlDbType.Int, cooperativeID);

            return db.ExecuteReader(dbCommand);
        }
        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int InsertHistory(int cooperativeID, int year, decimal charterCapital, decimal workingCapital, decimal fixedCapital, decimal turnover, decimal tax, decimal profit, int labourQuantity, int womanLabourQuantity, int cooperatorQuantity, int womanCooperatorQuantity, int partyMemberQuantity, string rank, int statusID)
        {
            History entity = new History();
            entity.CooperativeID = cooperativeID;
            entity.Year = year;
            entity.CharterCapital = charterCapital;
            entity.WorkingCapital = workingCapital;
            entity.FixedCapital = fixedCapital;
            entity.Turnover = turnover;
            entity.Tax = tax;
            entity.Profit = profit;
            entity.LabourQuantity = labourQuantity;
            entity.WomanLabourQuantity = womanLabourQuantity;
            entity.CooperatorQuantity = cooperatorQuantity;
            entity.WomanCooperatorQuantity = womanCooperatorQuantity;
            entity.PartyMemberQuantity = partyMemberQuantity;
            entity.Rank = rank;
            entity.StatusID = statusID;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_COOPERATIVE_History_Insert]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
            db.AddInParameter(dbCommand, "@CooperativeID", SqlDbType.Int, CooperativeID);
            db.AddInParameter(dbCommand, "@Year", SqlDbType.Int, Year);
            db.AddInParameter(dbCommand, "@CharterCapital", SqlDbType.Money, CharterCapital);
            db.AddInParameter(dbCommand, "@WorkingCapital", SqlDbType.Money, WorkingCapital);
            db.AddInParameter(dbCommand, "@FixedCapital", SqlDbType.Money, FixedCapital);
            db.AddInParameter(dbCommand, "@Turnover", SqlDbType.Money, Turnover);
            db.AddInParameter(dbCommand, "@Tax", SqlDbType.Money, Tax);
            db.AddInParameter(dbCommand, "@Profit", SqlDbType.Money, Profit);
            db.AddInParameter(dbCommand, "@LabourQuantity", SqlDbType.Int, LabourQuantity);
            db.AddInParameter(dbCommand, "@WomanLabourQuantity", SqlDbType.Int, WomanLabourQuantity);
            db.AddInParameter(dbCommand, "@CooperatorQuantity", SqlDbType.Int, CooperatorQuantity);
            db.AddInParameter(dbCommand, "@WomanCooperatorQuantity", SqlDbType.Int, WomanCooperatorQuantity);
            db.AddInParameter(dbCommand, "@PartyMemberQuantity", SqlDbType.Int, PartyMemberQuantity);
            db.AddInParameter(dbCommand, "@Rank", SqlDbType.NVarChar, Rank);
            db.AddInParameter(dbCommand, "@StatusID", SqlDbType.Int, StatusID);

            if (transaction != null)
            {
                db.ExecuteNonQuery(dbCommand, transaction);
                ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return ID;
            }
            else
            {
                db.ExecuteNonQuery(dbCommand);
                ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return ID;
            }
        }

        //---------------------------------------------------------------------------------------------
        public static bool InsertCollection(IList<History> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (History item in collection)
                        {
                            if (item.Insert(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at InsertCollection method: " + ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert / Update methods.

        public static int InsertUpdateHistory(int id, int cooperativeID, int year, decimal charterCapital, decimal workingCapital, decimal fixedCapital, decimal turnover, decimal tax, decimal profit, int labourQuantity, int womanLabourQuantity, int cooperatorQuantity, int womanCooperatorQuantity, int partyMemberQuantity, string rank, int statusID)
        {
            History entity = new History();
            entity.ID = id;
            entity.CooperativeID = cooperativeID;
            entity.Year = year;
            entity.CharterCapital = charterCapital;
            entity.WorkingCapital = workingCapital;
            entity.FixedCapital = fixedCapital;
            entity.Turnover = turnover;
            entity.Tax = tax;
            entity.Profit = profit;
            entity.LabourQuantity = labourQuantity;
            entity.WomanLabourQuantity = womanLabourQuantity;
            entity.CooperatorQuantity = cooperatorQuantity;
            entity.WomanCooperatorQuantity = womanCooperatorQuantity;
            entity.PartyMemberQuantity = partyMemberQuantity;
            entity.Rank = rank;
            entity.StatusID = statusID;
            return entity.InsertUpdate();
        }

        //---------------------------------------------------------------------------------------------

        public int InsertUpdate()
        {
            return this.InsertUpdate(null);
        }

        //---------------------------------------------------------------------------------------------

        public int InsertUpdate(SqlTransaction transaction)
        {
            const string spName = "p_COOPERATIVE_History_InsertUpdate";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
            db.AddInParameter(dbCommand, "@CooperativeID", SqlDbType.Int, CooperativeID);
            db.AddInParameter(dbCommand, "@Year", SqlDbType.Int, Year);
            db.AddInParameter(dbCommand, "@CharterCapital", SqlDbType.Money, CharterCapital);
            db.AddInParameter(dbCommand, "@WorkingCapital", SqlDbType.Money, WorkingCapital);
            db.AddInParameter(dbCommand, "@FixedCapital", SqlDbType.Money, FixedCapital);
            db.AddInParameter(dbCommand, "@Turnover", SqlDbType.Money, Turnover);
            db.AddInParameter(dbCommand, "@Tax", SqlDbType.Money, Tax);
            db.AddInParameter(dbCommand, "@Profit", SqlDbType.Money, Profit);
            db.AddInParameter(dbCommand, "@LabourQuantity", SqlDbType.Int, LabourQuantity);
            db.AddInParameter(dbCommand, "@WomanLabourQuantity", SqlDbType.Int, WomanLabourQuantity);
            db.AddInParameter(dbCommand, "@CooperatorQuantity", SqlDbType.Int, CooperatorQuantity);
            db.AddInParameter(dbCommand, "@WomanCooperatorQuantity", SqlDbType.Int, WomanCooperatorQuantity);
            db.AddInParameter(dbCommand, "@PartyMemberQuantity", SqlDbType.Int, PartyMemberQuantity);
            db.AddInParameter(dbCommand, "@Rank", SqlDbType.NVarChar, Rank);
            db.AddInParameter(dbCommand, "@StatusID", SqlDbType.Int, StatusID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------
        public static bool InsertUpdateCollection(IList<History> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (History item in collection)
                        {
                            if (item.InsertUpdate(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);

                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }
        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int UpdateHistory(int id, int cooperativeID, int year, decimal charterCapital, decimal workingCapital, decimal fixedCapital, decimal turnover, decimal tax, decimal profit, int labourQuantity, int womanLabourQuantity, int cooperatorQuantity, int womanCooperatorQuantity, int partyMemberQuantity, string rank, int statusID)
        {
            History entity = new History();
            entity.ID = id;
            entity.CooperativeID = cooperativeID;
            entity.Year = year;
            entity.CharterCapital = charterCapital;
            entity.WorkingCapital = workingCapital;
            entity.FixedCapital = fixedCapital;
            entity.Turnover = turnover;
            entity.Tax = tax;
            entity.Profit = profit;
            entity.LabourQuantity = labourQuantity;
            entity.WomanLabourQuantity = womanLabourQuantity;
            entity.CooperatorQuantity = cooperatorQuantity;
            entity.WomanCooperatorQuantity = womanCooperatorQuantity;
            entity.PartyMemberQuantity = partyMemberQuantity;
            entity.Rank = rank;
            entity.StatusID = statusID;
            return entity.Update();
        }

        //---------------------------------------------------------------------------------------------

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_COOPERATIVE_History_Update]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
            db.AddInParameter(dbCommand, "@CooperativeID", SqlDbType.Int, CooperativeID);
            db.AddInParameter(dbCommand, "@Year", SqlDbType.Int, Year);
            db.AddInParameter(dbCommand, "@CharterCapital", SqlDbType.Money, CharterCapital);
            db.AddInParameter(dbCommand, "@WorkingCapital", SqlDbType.Money, WorkingCapital);
            db.AddInParameter(dbCommand, "@FixedCapital", SqlDbType.Money, FixedCapital);
            db.AddInParameter(dbCommand, "@Turnover", SqlDbType.Money, Turnover);
            db.AddInParameter(dbCommand, "@Tax", SqlDbType.Money, Tax);
            db.AddInParameter(dbCommand, "@Profit", SqlDbType.Money, Profit);
            db.AddInParameter(dbCommand, "@LabourQuantity", SqlDbType.Int, LabourQuantity);
            db.AddInParameter(dbCommand, "@WomanLabourQuantity", SqlDbType.Int, WomanLabourQuantity);
            db.AddInParameter(dbCommand, "@CooperatorQuantity", SqlDbType.Int, CooperatorQuantity);
            db.AddInParameter(dbCommand, "@WomanCooperatorQuantity", SqlDbType.Int, WomanCooperatorQuantity);
            db.AddInParameter(dbCommand, "@PartyMemberQuantity", SqlDbType.Int, PartyMemberQuantity);
            db.AddInParameter(dbCommand, "@Rank", SqlDbType.NVarChar, Rank);
            db.AddInParameter(dbCommand, "@StatusID", SqlDbType.Int, StatusID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------
        public static bool UpdateCollection(IList<History> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (History item in collection)
                        {
                            if (item.Update(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at UpdateCollection method: " + ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int DeleteHistory(int id)
        {
            History entity = new History();
            entity.ID = id;

            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_COOPERATIVE_History_Delete]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static int DeleteBy_CooperativeID(int cooperativeID)
        {
            const string spName = "[dbo].[p_COOPERATIVE_History_DeleteBy_CooperativeID]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CooperativeID", SqlDbType.Int, cooperativeID);

            return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static bool DeleteCollection(IList<History> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (History item in collection)
                        {
                            if (item.Delete(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at DeleteCollection method: " + ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }
        #endregion
    }
}